
f_ind=ps_snip_time(:,4)==1;

s_ind=zeros(length(f_ind),1);
nns=find(act_type==3&act_ind(3,:)>1.35);
%nns=find(act_ind(3,:)-1>3*(max(act_ind([1 2 4],:))-1)&act_ind(3,:)>1.2);


for ind=1:length(nns)
    s_ind(ps_snip_time(:,13)==nns(ind))=1;
end
s_ind=logical(s_ind);

%s_ind(:)=1;
f_ind=f_ind&s_ind;


rsp=ps_snip_time(f_ind,12);
psact=mean(ps_snips(1501:2500,f_ind))-mean(ps_snips(1:1000,f_ind));
mact=mean(ps_snips(501:1000,f_ind));
ps_snip_sel=ps_snips(:,f_ind);


% high_ps_ind=mean(ps_snips(1501:2500,f_ind))-mean(ps_snips(1:1000,f_ind))>1;
% rsp=rsp(high_ps_ind,:);
% psact=psact(high_ps_ind);
% ps_snip_sel=ps_snip_sel(:,high_ps_ind);


% figure;
% plot(rsp,psact,'.')


% bins_start=[0.01:-0.005:-0.065];
% bins_end=[-0.0001:-0.005:-0.065];

bins_start=[0.0001 0:-0.005:-0.065];
bins_end=[-0.0001 -0.0101:-0.005:-0.065];


bin_resp=[];
ps_mean_bin=[];

figure;
hold on
for ind=1:9
    curr_ind=rsp<bins_start(ind)&rsp>bins_end(ind);
    bin_resp(ind)=mean(psact(curr_ind));
    bin_resp_sem(ind)=std(psact(curr_ind))/sqrt(sum(curr_ind));
    bin_resp_m(ind)=mean(mact(curr_ind));
    bin_resp_m_sem(ind)=std(mact(curr_ind))/sqrt(sum(curr_ind));
    ps_mean_bin(:,ind)=mean(ps_snip_sel(:,curr_ind)');
    plot(ps_mean_bin(:,ind)-mean(ps_mean_bin(501:1000,ind))+1,'color',[1 1 1]*(10-ind)/12)
    %plot(ps_mean_bin(:,ind),'color',[1 1 1]*(10-ind)/12)
    drawnow
end
%ylim([0.95 1.1])
xlim([501 6000])
ylabel('Fluorescence change [dF/F]')
xlabel('Time [s]')
set(gca,'xtick',[1000:1000:6000],'xticklabel',[0:5])


figure;
plot(bin_resp_m-1,'ro','linewidth',3,'markersize',8)
hold on
plot(bin_resp,'ko','linewidth',3,'markersize',8)



for ind=1:length(bin_resp)
    plot([1 1]*ind,[bin_resp_m(ind)-bin_resp_m_sem(ind) bin_resp_m(ind)+bin_resp_m_sem(ind)]-1,'r','linewidth',2)
    plot([1 1]*ind,[bin_resp(ind)-bin_resp_sem(ind) bin_resp(ind)+bin_resp_sem(ind)],'k','linewidth',2)
end

plot([0:length(bin_resp_m)],polyval(polyfit([1:length(bin_resp_m)],bin_resp_m,1),[0:length(bin_resp_m)])-1,'r')
plot([0:length(bin_resp)],polyval(polyfit([1:length(bin_resp)],bin_resp,1),[0:length(bin_resp)]),'k')

xlim([0.5 10.5])
set(gca,'xtick',[1:10],'xticklabel',[0:5:45])
xlabel('Running speed [cm/s]')
ylabel('Fluorescence change [dF/F]')
box off





figure;
hold on
cnt_a=0;
cnt_b=0;

for ind=1:33
    if AV_data(ind).layer==do_layer & AV_data(ind).ACode==1
        cnt_a=cnt_a+1;
        if sum(ps_snip_time(:,4)==1&ps_snip_time(:,9)==ind)>260
            cnt_b=cnt_b+1;
            %nn=length(unique(ps_snip_time(ps_snip_time(:,9)==ind,13)))
            ps_mean=mean(ps_snips(:,ps_snip_time(:,4)==1&ps_snip_time(:,9)==ind)');
            plot([-l_win:r_win],ps_mean-mean(ps_mean(1:1000))+1,'k','linewidth',1)
        end
        drawnow
        %title(num2str(sum(ps_snip_time(:,4)==1&ps_snip_time(:,9)==ind)));
    end
end

ylim([0.95 1.11]);
xlim([-500 5000])
box off
xlabel('Time [ms]')
ylabel('dF/F')

% 
% 
% cnt=0;
% for ind=1:length(AV_data)
%     disp(num2str(ind))
%     if AV_data(ind).layer==do_layer & AV_data(ind).ACode==1
%         for knd=1:size(AV_data(ind).raw_act,2)
%             if max(AV_data(ind).raw_act(:,knd))<100
%                 cnt=cnt+1;
%                 Nid(1,cnt)=ind;
%                 Nid(2,cnt)=knd;
%                 Nid(3,cnt)=AV_data(ind).cell_type(knd);
%             end
%         end
%     end
% end
% 
% 
% 
% 
% for ind=13:14
%     
%     [M N]=size(AV_data(ind).template);
%     
%     resp_map_a=zeros(M,N);
%     resp_map_b=zeros(M,N);
%     resp_map_c=zeros(M,N);
%     
%     curr_N=Nid(:,Nid(1,:)==ind);
%     curr_act_ind=act_ind(:,Nid(1,:)==ind);
%     
%     for knd=1:length(curr_N)
%         if curr_N(3,knd)==1
%             if curr_act_ind(3,knd)>1.05
%                 resp_map_a(AV_data(ind).indices{knd})=1;
%             elseif curr_act_ind(4,knd)>1.05
%                 resp_map_b(AV_data(ind).indices{knd})=1;
%             elseif curr_act_ind(2,knd)>1.05
%                 resp_map_c(AV_data(ind).indices{knd})=1;
%             end
%         end
%     end
%     
%     resp_map(:,:,1)=resp_map_a;
%     resp_map(:,:,2)=resp_map_b;
%     resp_map(:,:,3)=resp_map_c;
%     
%     figure;
%     imshow(AV_data(ind).template,[min(AV_data(ind).template(:)) max(AV_data(ind).template(:))])
%     hold on
%     hr=imshow(resp_map);
%     set(hr,'alphadata',0.4)
%     
% end
% 
% 










